package com.lxm.controller;


import com.lxm.common.tools.UserHolder;
import com.lxm.dto.BlogDTO;
import com.lxm.dto.Result;
import com.lxm.service.BlogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 发布表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2024-10-02
 */
@Tag(name = "文章管理")
@RestController
@RequestMapping("/user/blog")
public class BlogController {

    @Resource
    private BlogService blogService;

    /**
     * 新增文章
     *
     * @param blogDTO
     * @return
     */

    @PostMapping("/add")
    @Operation(summary = "添加文章")
    public Result add(@RequestBody BlogDTO blogDTO) {
        return blogService.add(blogDTO);
    }

    /**
     * 删除文章 可以批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/delete")
    @Operation(summary = "删除文章")
    public Result delete(@RequestBody List<Long> ids) {
        return blogService.batchDelete(ids);
    }


    /**
     * 根据用户id 查询已发布文章列表
     *
     * @param userId
     * @return
     */


    @Operation(summary = "查询博主的文章列表",
            description = "用于查询博主的其他文章或者自己的文章，按照时间排序")
    @GetMapping("/list")
    public Result list(@RequestParam(name = "userId", required = false) Long userId,
                       @RequestParam(name = "current", defaultValue = "1") Integer current) {

        return userId == null ? blogService
                .listByUserId(UserHolder.getUser().getUserId(), current)
                : blogService.listByUserId(userId, current);
    }

    /**
     * 文章修改
     *
     * @param blogDTO
     * @return
     */
    //有bug
    @Operation(summary = "修改文章")
    @PutMapping("/update/{blogId}")
    public Result update(@RequestBody BlogDTO blogDTO,@PathVariable Long blogId) {
        return blogService.updateBlog(blogDTO, blogId);
    }

    @Operation(summary = "获取按类型分类时间排序的博客列表", description = "查询展示")
    @GetMapping("/listByType")
    public Result listByType(@RequestParam(name = "type", defaultValue = "1") Integer type,
                             @RequestParam(name = "current", defaultValue = "1") Long current) {
        return blogService.listByType(type, current);
    }

    @Operation(summary = "获取按时间排序的博客列表", description = "首页查询展示")
    @GetMapping("/listByTime/firstPage")
    public Result listByTime(@RequestParam(name = "current", defaultValue = "1") Long current) {
        return blogService.listByTime(current);
    }

    //在redis中长期存储类型的Hash
    @Operation(summary = "获取博客的类型")
    @GetMapping("/type")
    public Result getBlogTypeList() {
        return blogService.getBlogTypeList();
    }

    @Operation(summary = "获取已发布文章的详细信息")
    @GetMapping("/detail/release/{blogId}")
    public Result getDetailBlogRelease(@PathVariable Long blogId) {
        return blogService.getDetailBlogRelease(blogId);
    }

    @Operation(summary = "获取自己的草稿文章的详细信息")
    @GetMapping("/detail/draftByMe")
    public Result getDetailBlogDraft() {
        return blogService.getDetailBlogDraftByME();
    }


}

